class HomeController < ApplicationController
  def index
    @slides = SlideImage.all
    @products = Product.at_home.includes(:categories).includes(:product_colors).includes(:brand)

    respond_to do |format|
      format.html
    end
  end

  def contact
    respond_to do |format|
      format.html
    end
  end

  def about

  end

  def submit_contact_form
    @cf = ContactForm.new
    @cf.name = params['inputName'].strip
    @cf.email = params['inputEmail'].strip
    @cf.phone = params['inputPhone'].strip
    @cf.message = params['inputMessage'].strip

    respond_to do |format|
      if @cf.valid?

        create_contact_emails(@cf)


        format.html { redirect_to successful_contact_url }
      else
        format.js
        format.html { render :contact }
      end
    end
  end

  def successful_contact

  end

  def technical_service

  end

  private

  def create_contact_emails(contact_form)
    eq = EmailQueue.new

    # Email for client
    eq.name_from = configatron.name_from
    eq.email_from = configatron.email_from
    eq.name_to = contact_form.name
    eq.email_to = contact_form.email
    eq.subject = configatron.contact_subject_for_client
    eq.body = build_contact_body_for_client(contact_form)

    eq.save

    eq = EmailQueue.new

    # Email for company
    eq.name_from = configatron.name_from
    eq.email_from = configatron.email_from
    eq.name_to = configatron.name_from
    eq.email_to = configatron.contact_to
    eq.subject = configatron.contact_subject_for_company
    eq.body = build_contact_body_for_company(contact_form)

    eq.save
  end

  def build_contact_body_for_company(contact_form)
    "
      <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
        <tr>
          <td>Se ha recibido la siguiente consulta:</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
            Nombre / Razón Social: " + contact_form.name + "
          </td>
        </tr>
        <tr>
          <td>
            Email: " + contact_form.email + "
          </td>
        </tr>
        <tr>
          <td>
            Teléfono: " + contact_form.phone + "
          </td>
        </tr>
        <tr>
          <td>
            Comentario:
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
            " + contact_form.message + "
          </td>
        </tr>
      </table>
"
  end

  def build_contact_body_for_client(contact_form)
    "
      <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
        <tr>
          <td>" + contact_form.name + ",</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
            Hemos recibido su consulta. La responderemos a la brevedad.
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
            Atentamente,
          </td>
        </tr>
        <tr>
          <td>
            Tressa Argentina
          </td>
        </tr>
      </table>
"
  end
end
